<?php
use yii\helpers\Html;
use yii\web\View;
use backend\models\activeRecords\Guild;
use kartik\widgets\ActiveForm;
use yii\helpers\Url;
use kartik\builder\FormGrid;
use kartik\builder\Form;
use common\libs\Constants;

/* @var $this View */
/* @var $model Guild */
/* @var $form ActiveForm */
?>
<div class="row game-select">
    <div class="col-sm-5">
        <div class="input-group">
            <span class="input-group-btn">
            </span>
        </div>
        <input type="hidden" id="guildSign" value="<?= $guildModel->guild_sign?>">
<!--        待选模块-->
        <div class="game-choose">
            <ul>
                <?php foreach($gameList as $key=>$value){?>
                    <li data-game-id="<?= $value->game_id?>">
                        <?= $value->game_name?>
                    </li>
                <?php }?>
            </ul>
        </div>
<!--        <select multiple size="20" class="form-control list" data-target="available"></select>-->
    </div>
    <div class="col-sm-2 game-brt-box">
        <div class="btn btn-success game-add">添加游戏>>></div>
        <div class="btn btn-primary game-remove"><<<移除游戏</div>
    </div>
    <div class="col-sm-5">
        <div class="game-chosen">
            <ul>
                <?php foreach($model as $value){?>
                    <li data-game-id="<?= $value->game_id?>">
                        <?= $value->game->game_name?>
                    </li>
                <?php }?>
            </ul>
        </div>
<!--        <select multiple size="20" class="form-control list" data-target="assigned"></select>-->
    </div>
</div>
<script>
    $(".game-choose ul li").click(function(){
        if($(this).hasClass("selected")){
            $(this).removeClass("selected")
        }
        else{
            $(this).addClass("selected")
        }

    });
    $(".game-chosen ul li").click(function(){
        if($(this).hasClass("selected")){
            $(this).removeClass("selected")
        }
        else{
            $(this).addClass("selected")
        }
    });
    var chooseList = [];
    var chosenList = [];
    var guildSign = $("#guildSign").val();
    $(".game-brt-box .btn").click(function(){
        if($(this).hasClass("game-add")){
            $(".game-choose ul li.selected").each(function(){
                $(".game-chosen ul").append(this);
                chooseList.push($(this).attr('data-game-id'));
            });
            $.ajax({
                url:'insert-game-list',
                type:'post',
                data:{gameList:chooseList,guildSign:guildSign},
                dataType:'text',
                success:function(data){
                    if(data){
                        chooseList = []
                    }
                }
            });
        }
        else {
            $(".game-chosen ul li.selected").each(function(){
                $(".game-choose ul").append(this);
                chosenList.push($(this).attr('data-game-id'));
            });
            $.ajax({
                url:'remove-game',
                type:'post',
                data:{gameList:chosenList,guildSign:guildSign},
                dataType:'text',
                success:function(data){
                    if(data == 1){
                        chosenList = [];
                    }
                }
            });
        }
    })
</script>